package com.yijinsuo.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.yijinsuo.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享
 *
 */
public class ShareSdkPopupWindow extends PopupWindow implements View.OnClickListener, PlatformActionListener {
    private View mMenuView;    //xml布局
    private Context context;     //上下文
    private String path;         //网友点进链接后，可以看到分享的详情（）
    private String headImage;   //分享图标
    private String content;     //分享文本
    public ShareSdkPopupWindow(Activity context, String content, String path, String headImage) {
        super(context);
        this.context = context;
        this.path = path;
        this.content = content;
        this.headImage = headImage;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.share_pop_window, null);
        RelativeLayout share_cancel = (RelativeLayout) mMenuView.findViewById(R.id.share_cancel);
        //取消按钮
        share_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //销毁弹出框
                dismiss();
            }
        });
        this.setContentView(mMenuView);
        // 设置背景，这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        this.setBackgroundDrawable(new BitmapDrawable());
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
//        this.setHeight(LayoutParams.MATCH_PARENT);

        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置允许在外点击消失
        this.setOutsideTouchable(false);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.ll_share).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

        LinearLayout iv_wx = (LinearLayout) mMenuView.findViewById(R.id.iv_wx);
        LinearLayout iv_py = (LinearLayout) mMenuView.findViewById(R.id.iv_py);
        LinearLayout iv_qq = (LinearLayout) mMenuView.findViewById(R.id.iv_qq);
        LinearLayout iv_qqkj = (LinearLayout) mMenuView.findViewById(R.id.iv_kj);


        iv_wx.setOnClickListener(this);
        iv_py.setOnClickListener(this);
        iv_qq.setOnClickListener(this);
        iv_qqkj.setOnClickListener(this);
        //1、分享的初始化
        ShareSDK.initSDK(context);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_wx:
                //2、设置分享内容
                Platform.ShareParams sp = new Platform.ShareParams();
                sp.setShareType(Platform.SHARE_WEBPAGE);//非常重要：一定要设置分享属性
                sp.setTitle(context.getResources().getString(R.string.share));  //分享标题
                sp.setText(content);   //分享文本
            //    sp.setImageUrl(headImage);//网络图片rul
                sp.setImagePath(headImage);
                sp.setUrl(path);   //网友点进链接后，可以看到分享的详情
                //3、非常重要：获取平台对象
                Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
                wechat.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                wechat.share(sp);
                break;
            case R.id.iv_py:
                //2、设置分享内容
                Platform.ShareParams py = new Platform.ShareParams();
                py.setShareType(Platform.SHARE_WEBPAGE); //非常重要：一定要设置分享属性
                py.setTitle(context.getResources().getString(R.string.share));  //分享标题
                py.setText(content);   //分享文本
               // py.setImageUrl(headImage);//网络图片rul
                py.setImagePath(headImage);
                py.setUrl(path);   //网友点进链接后，可以看到分享的详情
                //3、非常重要：获取平台对象
                Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
                wechatMoments.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                wechatMoments.share(py);
                break;
            case R.id.iv_qq:
                //2、设置分享内容
                Platform.ShareParams qs = new Platform.ShareParams();
                qs.setTitle(context.getResources().getString(R.string.share));
                qs.setText(content);
               // qs.setImageUrl(headImage);//网络图片rul
                qs.setImagePath(headImage);
                qs.setTitleUrl(path);  //网友点进链接后，可以看到分享的详情
                //3、非常重要：获取平台对象
                Platform qq = ShareSDK.getPlatform(QQ.NAME);
                qq.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                qq.share(qs);
                break;
            case R.id.iv_kj:
                //2、设置分享内容
                Platform.ShareParams qkj = new Platform.ShareParams();
                qkj.setTitle(context.getResources().getString(R.string.share));
                qkj.setText(content);
              //  qkj.setImageUrl(headImage);//网络图片rul
                qkj.setImagePath(headImage);
                qkj.setTitleUrl(path);  //网友点进链接后，可以看到分享的详情
                //3、非常重要：获取平台对象
                Platform qqkj = ShareSDK.getPlatform(QZone.NAME);
                qqkj.setPlatformActionListener(this); // 设置分享事件回调
                // 执行分享
                qqkj.share(qkj);
                break;
        }
        dismiss();
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
       if (platform.getName().equals(Wechat.NAME)) {
            handler.sendEmptyMessage(2);
        } else if (platform.getName().equals(WechatMoments.NAME)) {
            handler.sendEmptyMessage(3);
        } else if (platform.getName().equals(QQ.NAME)) {
            handler.sendEmptyMessage(4);
        }
       else if (platform.getName().equals(QZone.NAME)) {
           handler.sendEmptyMessage(7);
       }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        throwable.printStackTrace();
        Message msg = new Message();
        msg.what = 6;
        msg.obj = throwable.getMessage();
        handler.sendMessage(msg);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        handler.sendEmptyMessage(5);
    }


    Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            ShareSDK.stopSDK(context);
            switch (msg.what) {
                case 1:
                    Toast.makeText(context, "微博分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(context, "微信分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 3:
                    Toast.makeText(context, "朋友圈分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 4:
                    Toast.makeText(context, "QQ分享成功", Toast.LENGTH_LONG).show();
                    break;
                case 5:
                    Toast.makeText(context, "取消分享", Toast.LENGTH_LONG).show();
                    break;
                case 6:
                   // L.e("=================分享失败啊=====" + msg.obj);
                    Toast.makeText(context, "分享失败" + msg.obj, Toast.LENGTH_LONG).show();
                    break;
                case 7:
                    Toast.makeText(context, "QQ空间分享成功" + msg.obj, Toast.LENGTH_LONG).show();
                    break;
                default:
                    break;
            }
        }

    };
}
